package com.hx.likeserver.controller;

import com.hx.common.vo.Result;
import com.hx.likeserver.entity.Like;
import com.hx.likeserver.service.LikeMusicFeignService;
import com.hx.likeserver.service.LikeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/li/api")
public class LikeController {
    @Autowired
    private LikeService likeService;

    @ApiOperation(value = "添加喜欢")
    @PostMapping("/save")
    public Result save(Like like){
        boolean save1=likeService.save1(like);
        if (save1) {
            boolean save = likeService.save(like);
            return save?Result.ok():Result.fail("收藏失败");
        }
        return Result.fail("收藏重复");
    }

    @ApiOperation(value = "查询所有收藏")
    @GetMapping("/getall")
    public Result getAll(){
        List<Like> list=likeService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "删除收藏")
    @PostMapping("/delete")
    public Result delete(Like like){
        boolean removeById=likeService.removeById(like);
        return removeById?Result.ok():Result.fail("删除失败");
    }

    @Autowired
    private LikeMusicFeignService likeMusicFeignService;

    @ApiOperation(value = "通过ID查询音乐")
    @GetMapping("/likeMusic")
    public Result findMusicByUser(Integer musicId){
        return likeMusicFeignService.findMusicById(musicId);
    }

    @ApiOperation(value = "查询用户收藏")
    @GetMapping("/userLike")
    public Result findLikeByUser(Integer userId){
        List<Like> a = likeService.findLikeByUser(userId);
        System.out.println(a);
        List<Object> objects = new ArrayList<>();
        for (int i=0;i<a.size();i++){
            objects.add(likeMusicFeignService.findMusicById(a.get(i).getMusicId()).getData());
        }
        return Result.ok(objects);
    }
}
